package xuegao.study.mall.order.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xuegao.study.mall.common.api.CommonResult;
import xuegao.study.mall.common.dto.*;
import xuegao.study.mall.common.model.oms.OmsOrder;
import xuegao.study.mall.order.service.OrderService;

import java.util.List;

/**
 * 订单相关信息
 */
@Api(tags = "OmsOrderController")
@Tag(name = "OmsOrderController", description = "订单管理相关接口")
@RestController
@RequestMapping("/order")
public class OmsOrderController {

    @Autowired
    private OrderService orderService;

    @ApiOperation("批量删除订单")
    @PostMapping(value = "/create")
    public CommonResult create() {
        //todo 订单创建
        return CommonResult.failed();
    }


    @ApiOperation("批量删除订单")
    @PostMapping(value = "/delete")
    public CommonResult delete(@RequestParam("ids") List<Long> ids) {
        int count = orderService.delete(ids);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("批量关闭订单")
    @PostMapping(value = "/update/close")
    public CommonResult close(@RequestParam("ids") List<Long> ids, @RequestParam String note) {
        int count = orderService.close(ids, note);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("订单列表-后台")
    @GetMapping("/list")
    public List<OmsOrder> list(OmsOrderQueryParam queryParam,
                               @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                               @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        return orderService.list(queryParam, pageSize, pageNum);
    }

    @ApiOperation("订单详情-后台")
    @GetMapping(value = "/{id}")
    public OmsOrderDetail detail(@PathVariable("id") Long id) {
        return orderService.detail(id);
    }

    @ApiOperation("修改订单费用信息")
    @PostMapping(value = "/update/moneyInfo")
    public CommonResult updateMoneyInfo(@RequestBody OmsMoneyInfoParam param) {
        int count = orderService.updateMoneyInfo(param);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("修改收货人信息")
    @PostMapping(value = "/update/receiverInfo")
    public CommonResult updateReceiverInfo(@RequestBody OmsReceiverInfoParam param) {
        int count = orderService.updateReceiverInfo(param);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("备注订单-后台")
    @PostMapping(value = "/update/note")
    public CommonResult updateNote(@RequestParam("id") Long id,
                                   @RequestParam("note") String note,
                                   @RequestParam("status") Integer status) {
        int count = orderService.updateNote(id, note, status);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("批量发货")
    @PostMapping("/update/delivery")
    public CommonResult delivery(@RequestBody List<OmsOrderDeliveryParam> deliveryParamList) {
        int count = orderService.delivery(deliveryParamList);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
}
